﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OrionApplication.OrionLib.Dao;
using OrionApplication.OrionLib.Orion;
using OrionApplication.OrionLib;

namespace OrionApplication.OrionForm
{
    public partial class ClientForm : OrionBaseDoubleForm
    {
        public ClientForm() 
            : base()
        {
            dao = new DaoClient();
            InitializeComponent();
        }

        public ClientForm(string titre)
            : base(titre)
        {
            dao = new DaoClient();
            InitializeComponent();
        }

        private void DupliqueEnregistrement()
        {
            if (kDgv.SelectedRows.Count > 0)
            {
                if (OrionOutils.ChoixOuiNon(OrionConfBase.ConfirmDuplique) == DialogResult.Yes)
                {
                    Client c = ((DaoClient)dao).SelectById(Convert.ToInt32(kDgv.SelectedRows[0].Cells["OrionId"].Value));
                    c = ((DaoClient)dao).Insert(c);
                    this.RempliKDgv();
                    this.FocusSur(c.OrionId);
                }
            }
            else
                OrionOutils.Alerte(OrionConfBase.AucuneLigneSelectionnee);
        }

        private void RempliForm(Client c)
        {
            kCbQualite.Cb.SelectedValue = c.OrionQualiteId;
            kTbNom.Text = c.Nom;
            kTbPrenom.Text = c.Prenom;
            kTbCarteFidelite.Text = c.FidelCodeBarre;
            if (c.DateNaissance == null)
                KDTDateNaissance.Checked = false;
            else
            {
                KDTDateNaissance.Checked = true;
                KDTDateNaissance.Value = c.DateNaissance.Value;
            }
            kTbAdresse.Text = c.Adresse;
            kTbCP.Text = c.CP;
            kTbVille.Text = c.Ville;
            kTbPays.Text = c.Pays;
            kTbMail.Text = c.Mail;
            kTbTelephone.Text = c.Telephone;
            kTbPortable.Text = c.Portable;
            kTbCommentaire.Text = c.Commentaire;
        }

        private void SauvegardeForm()
        {
            Client om = this.RempliObject();
            if (kLblMode.Text == OrionConfBase.OrionModeInsert)
                om = ((DaoClient)dao).Insert(om);
            else
            {
                if (kDgv.SelectedRows.Count > 0)
                {
                    om.OrionId = Convert.ToInt32(kDgv.SelectedRows[0].Cells["OrionId"].Value);
                    om = ((DaoClient)dao).Update(om);

                }
                else
                    OrionOutils.Alerte(OrionConfBase.AucuneLigneSelectionnee);
            }
            this.RempliKDgv();
            this.FocusSur(om.OrionId);
        }

        private Client RempliObject()
        {
            Client c = new Client();
            c.OrionQualiteId = Convert.ToInt32(kCbQualite.Cb.SelectedValue);
            c.Nom = kTbNom.Text;
            c.Prenom = kTbPrenom.Text;
            c.FidelCodeBarre = kTbCarteFidelite.Text;
            if (KDTDateNaissance.Checked)
                c.DateNaissance = KDTDateNaissance.Value;
            else
                c.DateNaissance = null;
            c.Adresse = kTbAdresse.Text;
            c.CP = kTbCP.Text;
            c.Ville = kTbVille.Text;
            c.Pays = kTbPays.Text;
            c.Mail = kTbMail.Text;
            c.Telephone = kTbTelephone.Text;
            c.Portable = kTbPortable.Text;
            c.Commentaire = kTbCommentaire.Text;
            return c;
        }

        public void RowEnter()
        {
            if (this.NbLigneDgSelected > 0)
            {
                Client c = ((DaoClient)dao).SelectById(Convert.ToInt32(kDgv.SelectedRows[0].Cells["OrionId"].Value));
                this.RempliForm(c);
            }
        }

        private void btnEnvoiMail_Click(object sender, EventArgs e)
        {
            if (this.kTbMail.Text.Trim() != string.Empty)
                System.Diagnostics.Process.Start("mailto:" + this.kTbMail.Text);
        }

        private void buttonSpecAny1_Click(object sender, EventArgs e)
        {
            // Générer un n° de code barre ici
        }

        public void OuvreMouvementClient()
        {
            if (this.NbLigneDgSelected > 0 && this.kLblMode.Text == "MAJ")
            {
                MouvementLigneForm af = new MouvementLigneForm("Détail des ventes de " + kTbNom.Text + " " + kTbPrenom.Text);
                af.OrionClientId = this.OrionIdGdvSelected;
                af.MdiParent = this.MdiParent;
                af.NameFormA = "MouvementLigneForm";
                af.Show();
            }
            else
                OrionOutils.Alerte(OrionConfBase.AucuneLigneSelectionnee);
        }
    }
}
